package com.skyjing.rvadapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


abstract class CommonAdapter<T, V : ViewBinding>(val mContext: Context, val mDatas: List<T>) : RecyclerView.Adapter<ViewHolder>() {
    val NO_DATA = -1
    protected var inflate: ((LayoutInflater , ViewGroup , Boolean) -> V)? = null

    constructor(inflate: (LayoutInflater , ViewGroup , Boolean) -> V, mContext: Context, mDatas: List<T>) : this(mContext , mDatas) {
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (inflate == null) throw Exception("inflate must init")
        var v2 = inflate?.let { it(LayoutInflater.from(mContext) , parent , false) }
        return ViewHolder(v2!!, v2.root)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        convert(holder.v as V, mDatas[position], position)
    }

    abstract fun convert(v: V, t: T, position: Int)

    override fun getItemCount(): Int {
        return if (!mDatas.isNullOrEmpty()) mDatas.size else 0
    }

}